/*	BuildDepot class definition.
 *
*/

#include "BuildingPlacer.h"
#include "BuildCommandCenter.h"
#include <BWTA.h>

void BuildCommandCenter::Enter()
{
	DEBUG_PRINT("BuildBarracks(%p) %s entering\n", this, GetName().c_str());

}

void BuildCommandCenter::Execute()
{
	DEBUG_PRINT("BuildCommandCenter(%p) %s executing\n", this, GetName().c_str());
	Base* base = BB->GetBases()->front();
	std::list<BWAPI::Unit*>* scvs = (base)->GetUnitsByType(BWAPI::UnitTypes::Terran_SCV);
	std::list<BWAPI::Unit*>::iterator it = scvs->begin();	
	if (!builder)
	{
		for (; it!=scvs->end(); it++)
		{
			if (((*it)->isGatheringMinerals() || (*it)->isIdle()) && !(*it)->isCarryingMinerals()) break;
		}
		if(it==scvs->end())
		{
			control->FinishWithSuccess();
			return;
		}
		builder = (*it);

		if(BB->GetPossibleExpansions())
			target = (BB->GetPossibleExpansions()->front());// + BWAPI::TilePosition(2,-1);
		if(builder->getPosition().getApproxDistance(BWAPI::Position(target.x()*TILE_SIZE,target.y()*TILE_SIZE)) > 150)
		{
			builder->move(BWAPI::Position(target.x()*TILE_SIZE,target.y()*TILE_SIZE));
		}
		else
		{
			if(!ccTargetPicked)
			{
				BWAPI::TilePosition pos = BuildPlacer->getBuildLocationNear(target,BWAPI::UnitTypes::Terran_Command_Center,2);
				target = pos;
				ccTargetPicked = true;
			}		
			//target = BWTA::getNearestBaseLocation(target)->getTilePosition();
			builder->build(target,BWAPI::UnitTypes::Terran_Command_Center);
			BB->ResetOrder();
			DEBUG_PRINT("BuildCommandCenter(%p) attempting to build command center at tile position(%d,%d).\n", this, target.x(),target.y());
		}
	}
	else if(builder)
	{
		if(!builder->exists())
		{
			builder = NULL;
			BB->Refund(BWAPI::UnitTypes::Terran_Command_Center);
			target == BWAPI::TilePosition(-1,-1);
		}
		else if (builder->isConstructing()&& builder->getBuildUnit())
		{
			/*builder = NULL;
			BB->GetPossibleExpansions()->pop_front();
			ccTargetPicked = false;
			target == BWAPI::TilePosition(-1,-1);*/
		}
		else if (builder->getLastCommand().getType() == BWAPI::UnitCommandTypes::Move && !builder->getBuildUnit())
		{
			if(builder->getPosition().getApproxDistance(BWAPI::Position(target.x()*TILE_SIZE,target.y()*TILE_SIZE)) > 80)
			{
				builder->move(BWAPI::Position(target.x()*TILE_SIZE,target.y()*TILE_SIZE));
			}
			else
			{
				if(!ccTargetPicked)
				{
					target += (builder->getTilePosition() - target );
					BWAPI::TilePosition pos = BuildPlacer->getBuildLocationNear(target,BWAPI::UnitTypes::Terran_Command_Center);
					target = pos;
					ccTargetPicked = true;
				}		
				//target = BWTA::getNearestBaseLocation(target)->getTilePosition();
				builder->build(target,BWAPI::UnitTypes::Terran_Command_Center);
				builder = NULL;
				BB->GetPossibleExpansions()->pop_front();
				ccTargetPicked = false;
				target == BWAPI::TilePosition(-1,-1);
				BB->ResetOrder();
				DEBUG_PRINT("BuildCommandCenter(%p) attempting to build command center at tile position(%d,%d).\n", this, target.x(),target.y());
			}
		}
		else if(builder->getLastCommand().getType() == BWAPI::UnitCommandTypes::Gather)
		{
			//problem
			ccTargetPicked = false;
			target == BWAPI::TilePosition(-1,-1);
			builder = NULL;
			BB->Refund(BWAPI::UnitTypes::Terran_Command_Center);
		}
	}
	control->FinishWithSuccess();
}

void BuildCommandCenter::Exit()
{
	DEBUG_PRINT("BuildCommandCenter(%p) %s exiting\n", this, GetName().c_str());

}

bool BuildCommandCenter::CheckConditions()
{
	DEBUG_PRINT("BuildCommandCenter(%p) %s checking conditions\n", this, GetName().c_str());
	std::list<Base*>* bases = BB->GetBases();
	if(bases)
	{
		if(bases->size() < 3)
		{
			if(BWAPI::Broodwar->self()->supplyUsed()/(70*2) >= BB->GetBases()->size() || BWAPI::Broodwar->self()->cumulativeMinerals() >= ( 4200 * BB->GetBases()->size()))
			{
				if(BB->GetPossibleExpansions()->size() > 0)
				{
					if(BB->isBuildBuilding(BWAPI::UnitTypes::Terran_Command_Center))
					{
						DEBUG_PRINT("BuildCommandCenter(%p) %s checked conditions and has %d minerals to spend\n", this, GetName().c_str(), BB->GetMineralsToSpend());	
						BB->SpendResources(BWAPI::UnitTypes::Terran_Command_Center);
						return true;
					}
					else if(builder)
						return true;
				}
			}
		}
	}
	return false;

}